package cn.stylefeng.guns.modular.bi.service.impl;


import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.bi.entity.KnowledgeBase;
import cn.stylefeng.guns.modular.bi.mapper.KnowledgeBaseMapper;
import cn.stylefeng.guns.modular.bi.model.params.KnowledgeBaseParam;
import cn.stylefeng.guns.modular.bi.service.KnowledgeBaseService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 实施问题知识库 服务实现类
 * </p>
 */
@Service
public class KnowledgeBaseServiceImpl extends ServiceImpl<KnowledgeBaseMapper, KnowledgeBase> implements KnowledgeBaseService {

    @Autowired
    private UserService userService;
    @Override
    public List<KnowledgeBase> searchByTitle(String title) {
        return this.baseMapper.searchByTitle(title);
    }

    @Override
    public LayuiPageInfo findPageBySpec(KnowledgeBaseParam knowledgeBaseParam) {
        IPage<KnowledgeBase> page = new Page<>();
         // 设置分页参数
    if (knowledgeBaseParam != null) {
        if (knowledgeBaseParam.getPageNo() != null) {
            page.setCurrent(knowledgeBaseParam.getPageNo());
        }
        if (knowledgeBaseParam.getPageSize() != null) {
            page.setSize(knowledgeBaseParam.getPageSize());
        }
    }
    
    // 构建查询条件
    QueryWrapper<KnowledgeBase> queryWrapper = new QueryWrapper<>();
    if (knowledgeBaseParam != null && ToolUtil.isNotEmpty(knowledgeBaseParam.getTitle())) {
        queryWrapper.like("title", knowledgeBaseParam.getTitle());
    }
    
    // 添加排序，按创建时间倒序
    queryWrapper.orderByDesc("create_time");
    
    // 执行查询
    page = this.page(page, queryWrapper);
    
    // 包装成 LayuiPageInfo 返回
    LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
    layuiPageInfo.setCount(page.getTotal());
    layuiPageInfo.setData(page.getRecords());
    layuiPageInfo.setCode(0);
    layuiPageInfo.setMsg("查询成功");
    
    return layuiPageInfo;
    }

    @Override
    public void add(KnowledgeBaseParam knowledgeBaseParam) {
        KnowledgeBase knowledgeBase = getEntity(knowledgeBaseParam);
          // 自动填充创建时间、创建人、状态等信息
        Date now = new Date();
        knowledgeBase.setCreateTime(now);
        knowledgeBase.setUpdateTime(now);
        
        // 获取当前登录用户
        User currentUser = userService.getcurrentUser();
        if (currentUser != null) {
            knowledgeBase.setCreateBy(currentUser.getName());
            knowledgeBase.setUpdateBy(currentUser.getName());
        }
        
        // 设置默认状态为启用
        if (knowledgeBase.getStatus() == null) {
            knowledgeBase.setStatus(1); // 1表示启用
        }
        this.save(knowledgeBase);
    }

    @Override
    public void delete(KnowledgeBaseParam knowledgeBaseParam) {
        this.removeById(knowledgeBaseParam.getId());
    }

    @Override
    public void update(KnowledgeBaseParam knowledgeBaseParam) {
        KnowledgeBase newEntity = getEntity(knowledgeBaseParam);
        KnowledgeBase oldEntity = getOldEntity(knowledgeBaseParam);
        ToolUtil.copyProperties(newEntity, oldEntity);
        oldEntity.setUpdateTime(new Date());
        User currentUser = userService.getcurrentUser();
        if (currentUser != null) {
            oldEntity.setUpdateBy(currentUser.getName());
        }
        this.updateById(oldEntity);

    }

    private KnowledgeBase getEntity(KnowledgeBaseParam knowledgeBaseParam) {
        KnowledgeBase entity = new KnowledgeBase();
        ToolUtil.copyProperties(knowledgeBaseParam, entity);
        return entity;
    }

    private KnowledgeBase getOldEntity(KnowledgeBaseParam knowledgeBaseParam) {
        return this.getById(knowledgeBaseParam.getId());
    }

}